<?php
/**
 * author  :  hation
 *  email  : 331596350@qq.com
 */

namespace App\Http\Controllers\User;

use App\Http\Controllers\CommonController;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;
use Request;
use DB;
use Redirect;
use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;

/**
 * 用户信息控制器
 * Class UserController
 * @package App\Http\Controllers
 * note: 涉及用户资料管理等
 */
class UserController extends CommonController
{
    protected $user_table = 'user';

    public function __construct()
    {
        parent::__construct();
    }

    //用户登陆验证码
    public function captcha()
    {
        $builder = new CaptchaBuilder(null, new PhraseBuilder(4, "0123456789"));
        $builder->build(150, 40);
        $phrase = $builder->getPhrase();
        Session::put('captcha', $phrase);
        if (ob_get_length())
            ob_clean();
        return response($builder->output())->header('Content-type', 'image/jpeg');
    }

    public function ajaxSubmitCheck()
    {
        $input = Request::all();
//        dd($input);
        $check_code = Session::get('captcha');
        $check_user = object_to_array(DB::table('user')->
        where(array("account" => $input['account'], "password" => md5($input['password'])))->first());
        if ($input['code'] != $check_code) {
            $flag = 0; // 1为 验证通过 0为不通过 2用户名或密码错误
        } elseif (!empty($check_user)) {
            $flag = 1;
        } else {
            $flag = 2;
        }
        echo json_encode($flag, JSON_UNESCAPED_UNICODE);
    }

    /**
     * 用户登录页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function login()
    {
        return view('user/user/login');
    }

    public function little_login()
    {
        return view('user/user/little_login');
    }

    public function loginOut()
    {
        session()->flush();
        return Redirect::to('/');
    }

    /**
     * 登录操作逻辑处理
     * @return mixed
     */
    public function doLogin()
    {
        $request = Request::all();
        $params['account'] = $request['login_account'];
        $params['password'] = md5($request['login_pwd']);
        $re = object_to_array(DB::table($this->user_table)->where($params)->first());
        if (!empty($re)) {
            Session::put('uid', $re['id']);
            Session::put('username', $re['real_name']);
            Session::put('real_name', $re['real_name']);
            //  var_dump($re['review']);exit;
            Session::put('review', $re['review']);
            return Redirect::to('user/work/index');
        } else {
            return Redirect::to('user/user/login');
        }
    }

    public function changePassword()
    {
        return view('user/user/changePassword');
    }

    public function doChangePassword()
    {
        $uid = Session::get('uid');
        $input = Request::all();
        if (empty($input['old_pwd'])) {
            return back()->with('error', '旧密码为空');
        } elseif (empty($input['new_pwd'])) {
            return back()->with('error', '新密码为空');
        } elseif (empty($input['require_pwd'])) {
            return back()->with('error', '确认密码为空');
        } else {
            $data['password'] = md5($input['require_pwd']);
            $res = DB::table('user')->where(array("id" => $uid))->update($data);
            if ($res) {
                return back()->with('error', '修改密码成功');
            } else {
                return back()->with('error', '修改密码失败，请联系管理员');
            }
        }
    }

    public function checkPwd()
    {
        $uid = Session::get('uid');
        $input = Request::all();
        $res = object_to_array(DB::table('user')->
        where(array("id" => $uid, "password" => md5($input['old'])))->first());
        if (empty($res)) {
            $flag = 0; //flag 0 旧密码错误 1 旧密码正确
        } else {
            $flag = 1;
        }
        return json_encode($flag, JSON_UNESCAPED_UNICODE);
    }

    public function userInfo()
    {
        $uid = Session::get('uid');
        $user = object_to_array(DB::table('user')->
        where(array("id" => $uid))->first());
//        dump($user);exit();
        return view('user/user/userInfo')->with('data', $user);
    }

    public function doUserInfo()
    {
        $input = Request::all();
        $uid = Session::get('uid');
        $userdate = object_to_array(DB::table('user')->where(array("id" => $uid))->first());
        $data['phone'] = $input['tel'];
        $data['updated_at'] = date('Y-m-d h:i:s');
        $data['certificate_number'] = $input['code-num'];
        $data['real_name'] = $input['real-name'];
        $data['company'] = $input['company'];
        if (Input::has('file')) {
            if (!empty($userdate['certificate_photo'])) {
                if (is_file($userdate['certificate_photo'])) {
                    unlink($userdate['certificate_photo']);
                }
            }
            $dir = 'Uploads/userPic/';
            $file = Input::file('file');
            $filename = time() . '_' . mt_rand() . "." . $file->getClientOriginalExtension();
            if (!$file->move($dir, $filename)) {
                return back()->with('error', '导入失败');
            }
            $filePath = $dir . $filename;
            if (!empty($filePath)) {
                $data['certificate_photo'] = $filePath;
            }
        }
        if (!empty($data)) {
            $res = DB::table('user')->where(array("id" => $uid))->update($data);
            if ($res) {
                return back()->with('success', '修改成功');
            } else {
                return back()->with('error', '修改失败，请联系管理员');
            }
        } else {
            return back()->with('error', '数据传输错误，请联系管理员');
        }
    }
}
